import os
import sys
import pymysql
from bs4 import BeautifulSoup


class mysql:
    def __init__(self, host, database, user, pwd, port=3306, charset='utf8'):
        self.host = host
        self.db = database
        self.user = user
        self.pwd = pwd
        self.port = port
        self.charset = charset

    def getconnect(self):
        if not self.db:
            raise(NameError, "没有设置数据库信息")
        self.conn = pymysql.connect(
            host=self.host, user=self.user, passwd=self.pwd, db=self.db, port=self.port, charset=self.charset)
        cur = self.conn.cursor()
        if not cur:
            raise(NameError, "连接数据库失败")
        else:
            return cur

    def ExecQuery(self, sql):
        """
        执行查询语句
        返回的是一个包含tuple的list，list的元素是记录行，tuple的元素是每行记录的字段
        调用示例：
                ms = mysql(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics")
                resList = ms.ExecQuery("SELECT id,NickName FROM WeiBoUser")
                for (id,NickName) in resList:
                    print str(id),NickName
        """
        cur = self.getconnect()
        cur.execute(sql)
        resList = cur.fetchall()
        self.conn.close()
        return resList

    def ExecNoneQuery(self, sql):
        '''
        执行非查询语句
        '''
        cur = self.getconnect()
        cur.execute(sql)
        self.conn.commit()
        self.conn.close()


curPath = os.path.dirname(os.path.abspath(__file__))

# 分析
def analysis(filePath):
    soup = BeautifulSoup(open(filePath, 'r', encoding='utf8'), 'lxml')
    divs = soup.find_all('div', class_='productbox3')
    for div in divs:
        a = div.find('a', class_='productimage_1')
        id = a.get('href').strip().replace('showpdf.aspx?id=', '')
        img = a.find('img')
        url = img.get('src')

        if id and url:
            print('id:', id, 'url:', url)
            sql = "INSERT IGNORE INTO cover(id, url) VALUES('{}', '{}')".format(id,url)
            ms = mysql(host="192.168.0.92", user="root",
                       pwd="vipdatacenter", database="ebmbook")
            ms.ExecNoneQuery(sql)


def main():
    dirPath = os.path.join(curPath, 'detail')
    for path, dirNames, fileNames in os.walk(dirPath):
        for fileName in fileNames:
            filePath = os.path.join(path, fileName)
            print(filePath)
            analysis(filePath)


if __name__ == '__main__':
    main()
